/**
 * Created by Administrator on 15-10-20.
 */
var Core = require("./core.js");
var core = new Core();
var CheckOrderlist = RichBase.extend({
	EVENTS : {
		"click" : {
			"#tabMainCon .orderItem .checkBtn" : "onCheckBtnClick"
		},
		"focus" : {
			"#tabMainCon .orderItem .tinp" : "onCheckInpFocus"
		},
		"blur" : {
			"#tabMainCon .orderItem .tinp" : "onCheckInpBlur"
		}
	},
	//点击验证按钮
	onCheckBtnClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if(tarBtn.hasClass("success") || tarBtn.hasClass("disable") || tarBtn.hasClass("loading")) return false;
		var code = tarBtn.attr("data-code");
		var cmode = tarBtn.attr("data-cmode");
		var data = {};
		data["cmode"] = cmode;
		data["code"] = code;
		data["check"] = 1;
		data["salerid"] = core.getSalerid();
		data["ordernum"] = tarBtn.attr("data-mainordernum") || "";
		data["list"] = {};
		tarBtn.parents(".inCon").find(".ticketUl .ticketLi").each(function(){
			var item = $(this);
			var inp = item.find(".tinp");
			var ordernum = inp.attr("data-ordernum");
			var tnum = inp.val();
			data["list"][ordernum] = tnum;
		});
		data = PFT.Help.JSON.stringify(data);
		core.terminalCheck(data,{
			loading : function(){
				tarBtn.addClass("loading").text("验证中...");
			},
			removeLoading : function(){
				tarBtn.removeClass("loading").text("验证");
			},
			success : function(res){
				tarBtn.text("验证成功").addClass("success").next().html("");
				var orderItem = tarBtn.parents(".orderItem");
				orderItem.find(".oTit .flag").addClass("green").text("已使用");
				orderItem.find(".ticketUl .ticketLi").each(function(){
					var tarLi = $(this);
					var tarInp = tarLi.find(".tinp");
					tarLi.find(".checkText").text("已验证").next().text(tarInp.val());
				})
			},
			fail : function(res){
				var msg = res.msg || "验证失败，请稍后重试";
				var html = '<i class="iconfont" style="font-size:18px; margin-right:2px;">&#xe669;</i><span class="t">'+msg+'</span>';
				tarBtn.next().html(html);
				tarBtn.text("重新验证");
			},
			unlogin : function(){
				alert("登录状态已过期，请重新登录");
				window.location.href = "http://www.12301.cc/dlogin_n.html";
			},
			timeout : function(res){
				alert("请求起时，请稍后重试");
			},
			serverError : function(res){
				alert("请求出错，请稍后重试");
			}
		})
	},
	onCheckInpFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		tarInp.attr("data-lastval",val);
		return false;
	},
	onCheckInpBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var ptype = tarInp.attr("data-ptype").toUpperCase();
		var lastVal = tarInp.attr("data-lastval");
		var maxNum = tarInp.attr("data-maxnum");
		if(ptype=="F" || ptype=="C"){
			var ptypename = ptype=="F" ? "套票产品" : "酒店产品";
			if(val!=lastVal){
				tarInp.val(lastVal);
				alert(ptypename+"不得修改票数，须全部验掉");
				return false;
			}
		}
		if(!PFT.Help.isPositiveNum(val)) tarInp.val(lastVal);
		if(val*1>maxNum*1) tarInp.val(lastVal);
	}
});
module.exports = CheckOrderlist;